

//渠道
ascription2()
function ascription2() {
    $.ajax({
        type: "post",
        // url: 'https://'+ window.location.host +'/api/admin/apreorder/channelConfig/allChannelConfig',
        url: root.dockingplatform.allChannelConfig,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {

        },
        success: function (data) {
            console.log(data)
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#ascription_lsit").html(interText(dataInter));
            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}




/*获取收货地址*/
discountdata(123)
function discountdata (val) {
    if(val==123){
        //初始化 省
        var user_data = {
            level: '101',
            // memberId:'10258',
        }
        var succCallBack = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="1"){
                var tab_data=data.list;
                if(tab_data==''){
                    cer+='<option value=""></option>'
                    $("#getArea").html(cer);
                }else{
                    var cer="<option value=''>请选择收货区域</option>"
                    for(var i=0; i<tab_data.length; i++){
                        cer+='<option value="'+tab_data[i].address_id+'">'+tab_data[i].name+'</option>'
                    }
                    $("#getArea").html(cer);
                }
            }
        };
        web.doAjax(root.orderInter.order_return_address,'post',user_data, succCallBack);
    }else if(val==1){
        //市
        var special=$('#getArea').val()
        if(special==""){
            var cer="<option value=''>请选择收货位置</option>"
            $("#getPlace").html(cer);
        }else {
            var user_data = {
                level: '102',
                // memberId:'10258',
                addressIdStr:special
            }
            var succCallBack = function (data, status, response) {
                if(data.errorCode=="1"){
                    var tab_data=data.list;
                    if(tab_data==''){
                        cer+='<option value=""></option>'
                        $("#getPlace").html(cer);
                    }else{
                        var cer="<option value=''>请选择收货位置</option>"
                        for(var i=0; i<tab_data.length; i++){
                            cer+='<option value="'+tab_data[i].address_id+'">'+tab_data[i].name+'</option>'
                        }
                        $("#getPlace").html(cer);
                    }
                }
            };
            web.doAjax(root.orderInter.order_return_address,'post',user_data, succCallBack);
        }
    }
}
/*表格渲染*/
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];


function orderList(data) {
    dataSource.splice(0, dataSource.length);//清空数组
    if(data.data!=null){
    $.each(data.data.result, function (i, field) {
        var rowData = new Array();
        rowData.push(field.refundId);
        rowData.push(field.channelOrderId);
        rowData.push(field.orderId);
        rowData.push(field.refundSorce);
        rowData.push(field.refundReason);
        rowData.push(field.refundAmountFinal);
        rowData.push(field.createdAt);
        rowData.push(field.auditTime);
        rowData.push(field.listAddressReceiver[0]);
        rowData.push(field.refundStatus);
        dataSource.push(rowData);
    });}
    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "order": [[ 7, "desc" ]],
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },

                {
                    "render": function (data, type, row) {
                        var returnTypes = "";
                        switch (row[2]) {
                            case 10:
                                returnTypes = "买家";
                                break;
                            case 20:
                                returnTypes = "客服";
                                break;
                            case 30:
                                returnTypes = "门店";
                                break;
                        }
                        var val = '<span>' + returnTypes + '</span>';
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row) {
                        var returnType = "";
                        switch (row[3]) {
                            case 10:
                                returnType = "拒收";
                                break;
                            case 11:
                                returnType = "拒收-拦截";
                                break;
                            case 20:
                                returnType = "退货";
                                break;
                        }
                        var val = '<span>' + returnType + '</span>';
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {

                        var d = new Date(row[6]);
                        var val=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();

                        return val;
                    },
                    "targets":4
                },


                {
                    "render": function (data, type, row) {
                        if(row[7]==null){
                            var val=""
                            return val;
                        }else {
                            var d = new Date(row[7]);
                            var val=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();

                            return val;
                        }

                    },
                    "targets":5
                },
                {
                    "render": function (data, type, row) {
                        var hhh
                        if (row[8]==undefined || row[8]==null){
                            hhh=""
                        }else{
                            hhh=row[8].receiverName
                        }
                        var val = '<div>' + hhh + '</div>';
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var state = "";
                        switch (row[9]) {
                            case 10:
                                state = "待审核"
                                break;
                            // case 20:
                            //     state = "已受理"
                            //     break;
                            case 30:
                                state = "退货处理"
                                break;
                            case 40:
                                state = "退款处理"
                                break;
                            case 50:
                                state = "已完成"
                                break;
                            case 60:
                                state = "已关闭"
                                break;
                        }
                        var val = '<span>' + state + '</span>';
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span class=""><a class="list powerButton 621" onclick="orderInfo(\'申请单详情\',\'afterSaleService-returns.html\',\''+row[0]+'\',\''+row[2]+'\')">查看</a></span>';
                        return val;
                    },
                    "targets": 8
                }
            ]
        });
        //权限控制函数
        power()
    }
}
function getList(pagenum){
    var customerCode = ''
    if($("#ascription_lsit").val() == null){
        customerCode = '';
    }else{
        customerCode = $("#ascription_lsit").val()
    }

    $.ajax({
        contentType: "application/json",
        type: "POST",
        url:root.dockingplatform.queryPreRefundList,
        cache: false,  //禁用缓存
        dataType: "json",
        data: JSON.stringify({
           /* warehouse_ids:sessionStorage.getItem("regions"),//仓库权限串
            belong_groupIds:sessionStorage.getItem("companys"),//销售归属权限串
            sales_manager_ids:sessionStorage.getItem("personnels"),//销售经理权限串*/
            "pageNum":pagenum,
            "pageSize":50,
            "customerCode":customerCode,//渠道商id
            "refundId":$("#applyCode").val(),// 申请编号
            "channelOrderId":$("#dorderCode").val(),//对方单号
            "orderId":$("#orderCode").val(),//我方单号
            "refundSource":$("#applyType").val(),//退款方式
            "createdAtBegin":$("#applyTimeMin").val(),// 申请开始时间
            "createdAtEnd":$("#applyTimeMax").val(),// 申请结束时间
            "parentWarehouseId":$("#shippingWarehouse option:selected").val(),// 仓库id
            "refundStatus":$("#dealState").val(),//退货状态
            "refundReason":$("#reason").val(),//申请原因
            "auditTimeBegin":$("#checkTimeMin").val(),// 审核起始时间
            "auditTimeEnd":$("#checkTimeMax").val(),// 审核终止时间
            "salesManagerName":$("#saleManager").val(),// 销售经理名称
            "auditPersonName":$("#checker").val(),// 审核人
            "province": $("#getArea").val(),// 一级收货地（收货区域）
            "city":$("#getPlace").val(),// 二级收货地（收货位置）
            "channelSource":$("#channelSource").val(),
            "isRefundAbnormal":$("#isRefundAbnormal").val(),
            isRelated:$('#isRelated option:selected').val()//是否查询可关联售后单

        }),
        success: function (data) {
            sessionStorage.setItem("returnstatus",'')
            orderList(data);
            $('.M-box3').pagination({
                pageCount: data.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        contentType: "application/json",
                        type: "POST",
                        url:root.dockingplatform.queryPreRefundList,
                        cache: false,  //禁用缓存
                        dataType: "json",
                        data: JSON.stringify({
                            "customerCode":customerCode,//渠道商id
                            "refundId":$("#applyCode").val(),// 申请编号
                            "channelOrderId":$("#dorderCode").val(),//对方单号
                            "orderId":$("#orderCode").val(),//我方单号
                            "refundSource":$("#applyType").val(),//退款方式
                            "createdAtBegin":$("#applyTimeMin").val(),// 申请开始时间
                            "createdAtEnd":$("#applyTimeMax").val(),// 申请结束时间
                            "parentWarehouseId":$("#shippingWarehouse option:selected").val(),// 仓库id
                            "refundStatus":$("#dealState").val(),//退货状态
                            "refundReason":$("#reason").val(),//申请原因
                            "auditTimeBegin":$("#checkTimeMin").val(),// 审核起始时间
                            "auditTimeEnd":$("#checkTimeMax").val(),// 审核终止时间
                            "salesManagerName":$("#saleManager").val(),// 销售经理名称
                            "auditPersonName":$("#checker").val(),// 审核人
                            "province": $("#getArea").val(),// 一级收货地（收货区域）
                            "city":$("#getPlace").val(),// 二级收货地（收货位置）
                            "pageNum":api.getCurrent(),
                            "pageSize":50,
                            "channelSource":$("#channelSource").val(),
                            "isRefundAbnormal":$("#isRefundAbnormal").val(),
                            isRelated:$('#isRelated option:selected').val()//是否查询可关联售后单
                        }),
                        success: function (result) {
                            orderList(result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

/*跳转订单详情*/
function orderInfo(title,url,returnId,orderId){
    sessionStorage.setItem("returnId",returnId);
    sessionStorage.setItem("returnOrderId",orderId);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//弹框跳转
var returnstatus= sessionStorage.getItem("returnstatus")
if(returnstatus==10){
    $('#dealState').val(returnstatus)
    getList(1)
}